package service.impl;

import dao.KnowledgeDao;
import dao.impl.KnowledgeDaoImpl;
import pojo.Knowledge;
import pojo.vo.Message;
import service.KnowledgeService;

import java.util.List;

/**
 * @author wq3stone
 */
public class KnowledgeServiceImpl implements KnowledgeService {
    KnowledgeDao knowledgeDao = new KnowledgeDaoImpl();

    @Override
    public Message insertKlg(Knowledge knowledge) {
        Message message = new Message();
        try {
            knowledgeDao.insert(knowledge);
            message.setCode(0);
            message.setMsg("新增知识成功");
            message.setObj(null);
        } catch (Exception e){
            e.printStackTrace();
            message.setCode(1);
            message.setMsg("新增知识失败");
            message.setObj(null);
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public Message deleteKlg(String key,String value) {
        Message message = new Message();
        try {
            knowledgeDao.delete(key,value);
            message.setCode(0);
            message.setMsg("删除知识成功");
            message.setObj(null);
        } catch (Exception e){
            e.printStackTrace();
            message.setCode(1);
            message.setMsg("删除知识失败");
            message.setObj(null);
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public Message updateKlg(Knowledge knowledge) {
        Message message = new Message();
        try{
            knowledgeDao.update(knowledge);
            message.setCode(0);
            message.setMsg("更新成功");
            message.setObj(knowledge);
        } catch (Exception e){
            message.setCode(1);
            message.setMsg("更新失败");
            message.setObj(knowledge);
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public Message queryKlg(String key, String value) {
        Message message = new Message();
        try{
            List<Knowledge> list = knowledgeDao.query(key, value);
            message.setCode(0);
            message.setMsg("查询成功");
            message.setObj(list);
        } catch (Exception e){
            message.setCode(1);
            message.setMsg("查询失败");
            message.setObj(null);
            e.printStackTrace();
        }
        return message;
    }
    @Override
    public Message queryKlg(String key, String value, int uid) {
        Message message = new Message();
        try{
            List<Knowledge> list = knowledgeDao.query(key, value,uid);
            message.setCode(0);
            message.setMsg("查询成功");
            message.setObj(list);
        } catch (Exception e){
            message.setCode(1);
            message.setMsg("查询失败");
            message.setObj(null);
            e.printStackTrace();
        }
        return message;
    }
}
